<?php 
/**
 * @version        $Id$
 * @author         master@xuewl.com
 * @copyright      Copyright (c) 2007 - 2014, Chongqing xuewl Information Technology Co., Ltd.
 * @link           http://www.xuewl.com
**/
namespace Document\Controller;
use \Common\Controller\BaseController;
class NewtaskController extends BaseController {
    public function _initialize() {
    	parent::_initialize();
        $this->models = getcache('model', 'commons');
        $this->userid = (int) cookie('_userid');
        $this->db = D('Newtask');
        /* 将页码变量赋值到模板 */
    }

    public function index($id = '') {
        $id  = (int) $id;
    	$SEO = seo('新手任务');
        $sqlmap = array();
        $sqlmap['status'] = 1;
        if ($id > 0) {
            $sqlmap['id'] = $id;
        }
        $info = $this->db->where($sqlmap)->order("dateline DESC")->find();
        $task_list = $this->db->limit(3)->order("dateline DESC")->select();
        if(!$info) $this->error('该任务不存在');
        $info['configs'] = string2array($info['configs']);
        $document = array();
        foreach ($info['configs'] as $key => $value) {
            $v = $value;
            if($value['task_type'] == 'cash') {
                $v['title'] = '提现';
                $v['thumb'] = THEME_STYLE_PATH.'style/images/newcomers/cash.gif';
                $v['url'] = U('Member/Account/cash');
                /* 判断状态 */
                $v['is_finish'] = FALSE;
                if ($this->userid > 0) {
                    $sqlmap = array();
                    $sqlmap['apply_coin'] = $value['task_id'];
                    $sqlmap['userid'] = $this->userid;
                    $sqlmap['cash_status'] = 1;
                    $v['is_finish'] = (M('Cash')->where($sqlmap)->count()) ? TRUE : FALSE;
                }
            } else {
                list($modelid, $docid) = explode('-', $value['task_id']);
                $model = $this->models[$modelid];
                if(!$modelid || !$docid || !$model) continue;
                $row = M($model['tablename'])->getById($docid);
                if(!$row) continue;
                foreach ($row as $key => $value) {
                    if(empty($value)) unset($row[$key]);
                }
                $v = array_merge($v, $row);
                if ($this->userid > 0) {
                    $sqlmap = array();
                    $sqlmap['taskid'] = $value['task_id'];
                    $sqlmap['userid'] = $value['userid'];
                    if ($value['extra']) {
                        $sqlmap['extra'] = $value['extra'];
                    }
                    $v['is_finish'] = (M('Score')->where($sqlmap)->count()) ? TRUE : FALSE;
                }                                
            }
            $document[] = $v;
        }
        $catid = 53;
    	include template('newtask_index');
    }

    /**
     * 服务条款
     */
    public function telluserRule(){
        include template('telluserRule');
    }
}
?>